package com.example.fresh_market.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.fresh_market.adapter.ProductAdmAdapter;
import com.example.fresh_market.databinding.ActivityProductBinding;
import com.example.fresh_market.entity.Product;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.request.PageResult;
import com.example.fresh_market.utils.HttpUtil;
import com.example.fresh_market.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class ProductActivity extends AdminLoginActivity {

    private final int requestCode = 11;
    private ActivityProductBinding binding;
    private Context mContext;
    private ProductAdmAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityProductBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = this;
        adapter = new ProductAdmAdapter(new ProductAdmAdapter.OnItemClickListener() {
            @Override
            public void onEditClick(View view, int position) {
                Product product = adapter.getItem(position);
                Intent intent = new Intent(mContext, ProductEditActivity.class);
                String json = new Gson().toJson(product);
                Log.d("!23", "onEditClick: " + json);
                intent.putExtra("product", json);
                startActivityForResult(intent, requestCode);
            }

            @Override
            public void onDeleteClick(View view, int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("删除商品");
                builder.setMessage("确定删除该商品吗？");
                builder.setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                });
                builder.setPositiveButton("确定", (dialog, which) -> {
                    Product product = adapter.getItem(position);
                    String delUrl = HttpUtil.getBatchDelUrl("/admin/product", List.of(product.getId()));
                    HttpUtil.delete(mContext, delUrl, response -> {
                        ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                        if (apiResponse.getCode() == 200) {
                            ToastUtil.showToast(mContext, "删除成功");
                            init();
                        }
                    });
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }

            @Override
            public void onShowClick(View view, int position) {
                Product product = adapter.getItem(position);
                HttpUtil.get(mContext, "/admin/product/status/" + product.getId(), response -> {
                    ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                    if (apiResponse.getCode() == 200) {
                        init();
                    }
                });
            }
        }, mContext);
        RecyclerView productList = binding.productList;
        productList.setLayoutManager(new LinearLayoutManager(mContext));
        productList.setAdapter(adapter);
        init();
        productList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {

            }
        });
        binding.addProduct.setOnClickListener(view -> {
            startActivityForResult(new Intent(mContext, ProductEditActivity.class), requestCode);
        });
    }

    private void init() {
        HttpUtil.get(mContext, "/admin/product?page=1&size=100000", response -> {
            Gson gson = new Gson();
            ApiResponse<PageResult<Product>> apiResponse = gson.fromJson(response, new TypeToken<ApiResponse<PageResult<Product>>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                adapter.submitList(apiResponse.getData().getList());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == this.requestCode && resultCode == RESULT_OK) {
            init();
        }
    }
}
